package com.teamTPZoo.Util;

import com.teamTPZoo.Constants.ConstantsIF;

import org.apache.log4j.Logger;

import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Strings {
	private Logger logger = Logger.getLogger(getClass());
	
	public String strRepeat( String masterString, int repeatCount )
	{
		String returnValue = "";
		
		try {
			for ( int cnt=0; cnt<repeatCount; cnt++ ) {
				returnValue += masterString;
			}
		} catch (Exception e) {
			logger.debug( e.toString() );
		}
		
		return returnValue;
	}
	
	/**
	 * 스크립트태그값 모두 제거
	 * @param src
	 * @return
	 */
	public String stripScript( String src ) 
	{
		try {
			Pattern p = Pattern.compile("<(script|SCRIPT|Script)[^>]*>\\S*</(script|SCRIPT|Script)>", Pattern.UNICODE_CASE);
		    Matcher m = p.matcher( src );
	
		    if ( m.find() ) {
		    	src = m.replaceAll("").trim();
			}
		} catch (Exception e) {
			logger.debug( e.toString() );
		}
	    return src;
	}
	
    /**
     * URL의 문자열을 인코딩하여 리턴
     * @param str URL문자열
     * @param charSet 인코딩할 charSet (UTF-8, MS949...)
     * @return String
     */
    public String encodeURL(String str, String charSet)
    {
        String result = null;

        try {
            if( str != null ) {
                result = URLEncoder.encode(str, charSet==null?ConstantsIF.CHARSET_UTF8:charSet);
            }
        }
        catch (Exception e) {
        	logger.debug( e.toString() );
        }

        return result;
    }

    /**
     * 인코딩된 URL의 문자열을 디코딩(한글화)하여 리턴
     * @param str URL문자열
     * @param charSet 디코딩할 charSet (UTF-8, MS949...)
     * @return String
     */
    public String decodeURL(String str, String charSet)
    {
        String returnValue = null;
        
        try {
            if( str != null ) {
            	returnValue = URLDecoder.decode(str, charSet==null?ConstantsIF.CHARSET_UTF8:charSet );
            }
        }
        catch (Exception e) {
        	logger.debug( e.toString() );
        }
        
        return returnValue;
    }

    public static String nvl( String inputString )
    {
    	return (inputString!=null?inputString:"");
    }
    
    public static String nvl( String inputString, String defaultString )
    {
    	return (inputString!=null?inputString:defaultString);
    }
    
    
    /**
     * 첫번째 문자가 소문자일 경우 대문자로 변경
     * @param str
     * @return
     */
      public static String strInitCap(String str)
      {
          String second = str.substring(1);
          char   h      = str.charAt(0);
          
          if ( h>='a' && h<='z') {
              String first = String.valueOf((char) (h - 32));
              return first + second;
          }
          else {
              return str;
          }
      }    
      
      public static String nl2br( String strSource )
      {
          if ( strSource!= null )    return strSource.replaceAll("\r", "<br>");
          return strSource;
      }

      public static String cutString( String strSource, int cutLength )
      {
          byte [] strByte     = strSource.getBytes();
          int     cnt         = 0;
          String  returnValue = "";
          
          if( strByte.length < cutLength )    return strSource;
          
          for( int i = 0; i < cutLength; i++ ) {
              if( strByte[i] < 0 )    cnt++;
          }

          if ( cnt%2==0 )    returnValue = new String(strByte, 0, cutLength );
          else               returnValue = new String(strByte, 0, cutLength + 1 );

          return returnValue;
      }
      
}
